﻿using System;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.Win.Controls;
using PlanningGIS.Engine.Controls;
using PlanningGIS.ArcGIS;
using System.IO;

namespace PlanningGIS.Product
{
    /// <summary>
    /// 元数据创建(一部移植功能)
    /// </summary>
    public partial class FrmMdCreate : Office2007Form
    {
        /// <summary>
        /// 日志
        /// </summary>
        private LogPanel logPanel;
        /// <summary>
        /// 
        /// </summary>
        private IApplication m_Application;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="value">地图对象</param>
        public FrmMdCreate(IApplication value)
        {
            InitializeComponent();
            m_Application = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMdCreate_Load(object sender, EventArgs e)
        {
            cbo_metadataType.SelectedIndex = 0;
            cbo_databaseType.SelectedIndex = 0;

            #region 弹出日志
            IDockManager frameController = (m_Application as IFramework).DockManager;
            logPanel = (m_Application as IFramework).GetControlUserData("日志") as LogPanel;
            if (logPanel == null)
            {
                logPanel = new LogPanel();
                frameController.AddDockPanel("日志", logPanel, false, esriDockFlags.esriDockBottom);
            }
            frameController.ActivateDockPanel("日志");
            #endregion
        }
        /// <summary>
        /// 打开本地数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenLoaclDb_Click(object sender, EventArgs e)
        {
            try
            {
                OpenDataDialog selectData = new OpenDataDialog();
                selectData.MultiSelect = false;
                if (selectData.ShowDialog(this) != DialogResult.OK) return;
                string strPath = selectData.FullName.TrimEnd('\\');
                if (strPath.ToLower().EndsWith(".gdb") || strPath.ToLower().EndsWith(".mdb"))
                {
                    txtLoaclDbPath.Text = selectData.FullName;
                    selectData.Close();
                }
                else
                {
                    MsgHelper.ShowWarningMsgbox("请选择空间数据库（GDB、MDB）数据！");
                    return;
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox(ex.Message);
            }
        }
        private void btnMDCreate_Click(object sender, EventArgs e)
        {
            #region 1条件判断
            PlanningGISMetadataType metadataType;//元数据类型
            DataStructure dataStructure = null;//空间数据结构类型
            //PlanningGISDataScale dataScale;//比例尺类型
            PlanningGISDatabaseType databaseType;//数据库分类
            DataBaseNode DBNode = new DataBaseNode();
            DBNode.WorkspaceType = zjgisWorkspaceType.gdb;
            FeatureClassNode FCNode = new FeatureClassNode();
            FCNode.WorkspaceType = zjgisWorkspaceType.gdb;

            switch (cbo_databaseType.SelectedItem.ToString())
            {
                case "基础空间要素数据库(DLG库)":
                    databaseType = PlanningGISDatabaseType.DLG;
                    break;
                case "数字高程模型数据库(DEM库)":
                    databaseType = PlanningGISDatabaseType.DEM;
                    break;
                case "数字正射影像数据库(DOM库)":
                    databaseType = PlanningGISDatabaseType.DOM;
                    break;
                case "元数据数据库(MD库)":
                    databaseType = PlanningGISDatabaseType.MD;
                    break;
                case "地名数据库(PN库)":
                    databaseType = PlanningGISDatabaseType.PN;
                    break;
                case "大地成果数据库DDK库)":
                    databaseType = PlanningGISDatabaseType.DDK;
                    break;
                case "系统维护数据库(MT库)":
                    databaseType = PlanningGISDatabaseType.MT;
                    break;
                case "三维景观数据库(D3库)":
                    databaseType = PlanningGISDatabaseType.D3;
                    break;
                default: MsgHelper.ShowErrorMsgbox("请选择数据库类型！"); return;
            }

            switch (cbo_metadataType.SelectedItem.ToString())
            {
                case "数据库级元数据":
                    metadataType = PlanningGISMetadataType.DATABASE_LEVEL;
                    //metadataFCName = metadataFCName+"_DB";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISDatabaseMetadata), databaseType);
                    break;
                case "DLG测区级元数据":
                    metadataType = PlanningGISMetadataType.REGION_LEVEL_DLG;
                    //metadataFCName = metadataFCName + "_RG";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISDLGRegionMetadata), databaseType);
                    break;
                case "DEM测区级元数据":
                    metadataType = PlanningGISMetadataType.REGION_LEVEL_DEM;
                    //metadataFCName = metadataFCName + "_RG";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISDEMRegionMetadata), databaseType);
                    break;
                case "DOM测区级元数据":
                    metadataType = PlanningGISMetadataType.REGION_LEVEL_DOM;
                    //metadataFCName = metadataFCName + "_RG";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISDOMRegionMetadata), databaseType);
                    break;
                case "图幅级元数据":
                    metadataType = PlanningGISMetadataType.MAPSHEET_LEVEL;
                    //metadataFCName = metadataFCName+"_MS";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISMapsheetMetadata), databaseType);
                    break;
                case "要素级元数据":
                    metadataType = PlanningGISMetadataType.FEATURE_LEVEL;
                    //metadataFCName = metadataFCName+"_FT";
                    dataStructure = MetadataHelper.getMetadataDefaultDef(typeof(PlanningGISFeatureMetadata), databaseType);
                    break;
                default: MsgHelper.ShowErrorMsgbox("请选择元数据类型！"); return;
            }

            if (txtLoaclDbPath.Text.Trim() == "")
            {
                MsgHelper.ShowErrorMsgbox("请设置空间数据库！");
                return;
            }

            FCNode.Name = ((PlanningGISFeatureclass)dataStructure).name;
            FCNode.Path = txtLoaclDbPath.Text;
            DBNode.Path = txtLoaclDbPath.Text;
            IWorkspaceFactory2 wsf = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();  
            if (Path.GetExtension(txtLoaclDbPath.Text).Equals(".MDB",StringComparison.CurrentCultureIgnoreCase))
            {
                DBNode.WorkspaceType = zjgisWorkspaceType.mdb;
                FCNode.WorkspaceType = zjgisWorkspaceType.mdb;
                wsf = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
            }
            DBNode.PropertySet = wsf.ReadConnectionPropertiesFromFile(txtLoaclDbPath.Text);
            FCNode.PropertySet = DBNode.PropertySet;
            IDataset targetDS = DBNode.Open();
            IFeatureWorkspace fws = targetDS as IFeatureWorkspace;
            if(fws!=null)
            {
                if (GeodatabaseHelper.ExistsFeatureClass(DBNode.Workspace, FCNode.Name))
                {
                    MsgHelper.ShowErrorMsgbox("空间数据库中已存在元数据要素类：" + FCNode.Name + "！");
                    return;
                }
            }
            #endregion 1条件判断

            #region 2元数据要素类创建
            try
            {
                Cursor = Cursors.WaitCursor;
                Application.DoEvents();
                CommonClass.CreateFeatCls(fws, null, dataStructure as PlanningGISFeatureclass);
                //GeodatabaseHelper.CreateFeatureClass(fws, (dataStructure as PlanningGISFeatureclass).name, dataStructure as PlanningGISFeatureclass);
                logPanel.DoneLog("元数据要素类[" + FCNode.Name + "]创建完毕。\n");
                MsgHelper.ShowInfomationMsgbox("元数据要素类[" + FCNode.Name + "]创建完毕。");
            }
            catch (Exception ex)
            {
                MsgHelper.ShowWarningMsgbox("元数据要素类[" + FCNode.Name + "]创建失败：" + ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            #endregion 2元数据要素类创建
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbo_metadataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbo_metadataType.SelectedItem.ToString() == "DLG测区级元数据")
            {
                cbo_databaseType.SelectedIndex = getObjCollectionIndex(cbo_databaseType.Items, "基础空间要素数据库(DLG库)");
            }
            else if (cbo_metadataType.SelectedItem.ToString() == "DEM测区级元数据")
            {
                cbo_databaseType.SelectedIndex = getObjCollectionIndex(cbo_databaseType.Items, "数字高程模型数据库(DEM库)");
            }
            else if (cbo_metadataType.SelectedItem.ToString() == "DOM测区级元数据")
            {
                cbo_databaseType.SelectedIndex = getObjCollectionIndex(cbo_databaseType.Items, "数字正射影像数据库(DOM库)");
            }
        }

        private int getObjCollectionIndex(ComboBox.ObjectCollection pItems, string pString)
        {
            for (int i = 0; i < pItems.Count; i++)
            {
                if (pItems[i].ToString() == pString)
                {
                    return i;
                }
            }

            return -1;
        }
    }
}
